package lexicon;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import jcolibri.extensions.textual.stemmer.Stemmer;
import jcolibri.extensions.textual.stemmer.Stemmer.Language;

public class LexiconLoader {
	
	private BufferedReader br;
	
	public LexiconLoader(String route){
		try {
			File file = new File(route);

			FileReader fr = new FileReader(file);
			
			br = new BufferedReader(fr);
			
		} catch (FileNotFoundException e) {
	
			e.printStackTrace();
		}
	}
	/*
	 * Returns hashMap, for each word 1 if positive, -1 if negative
	 */
	public HashMap<String, Integer> load(){
		
		String line;
		HashMap<String, Integer> ret = new HashMap<String, Integer>();
		try {
			
			while((line=br.readLine())!=null){
				
				String word, number, valoration1;
				String[] words;
				
				words = line.split("\t");
				
				word = words[0];
				number = words[1];
				
				valoration1 = words[2];
				
				int pos = valoration1.indexOf(" ", 0);
				int result = 0;
				
				if (pos > 0){
					valoration1 = valoration1.substring(0, pos);
				}					

				if (valoration1.equals("pos"))
					result = 1;
				else
					result = -1;
				Stemmer ste = new Stemmer(Language.SPANISH);
				
				ret.put(ste.stemSentence(word), result);
				//ret.put(word, result);

						
				//System.out.println(word +" | "+valoration1+" | ");
				
			}
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		//System.out.println(ret.toString());
		return ret;
		
	}

	
}
